<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="../../../static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../static/css/admin.css" media="all">
    <style>
        .layui-table{
            text-align: center!important;
        }
    </style>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="border-radius: 0;border-top: 4px solid #d2d2d2;">
            <div class="layui-form-item">

                <div class="layui-inline" style="margin-left: -40px">
                    <label class="layui-form-label">职位名称</label>
                    <div class="layui-input-inline" style="width: 150px!important;">
                        <input type="text" name="job_name" autocomplete="off" class="layui-input" >
                    </div>
                </div>
                <div class="layui-inline" style="margin-left: -50px">
                    <label class="layui-form-label">职位分类</label>
                    <div class="layui-input-inline" style="width: 100px!important;">
                        <select name="job_type" >
                            <option value=""></option>
                            <option value="技术">技术</option>
                            <option value="实施">实施</option>
                            <option value="测试">测试</option>
                            <option value="售前">售前</option>
                            <option value="行政">行政</option>
                            <option value="财务">财务</option>
                            <option value="管理">管理</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline" style="margin-left: -50px; margin-top: 10px">
                    <div class="layui-inline">
                        <label class="layui-form-label">登记日期</label>
                        <div class="layui-inline" id="test6" >
                            <div class="layui-input-inline" style="width: 100px!important;">
                                <input type="text" name="regist_time" autocomplete="off" id="test-startDate-1" class="layui-input">
                            </div>
                            <div class="layui-form-mid">截止日期</div>
                            <div class="layui-input-inline" style="width: 100px!important;">
                                <input type="text" name="deadline" autocomplete="off" id="test-endDate-1" class="layui-input" >
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-jobPost-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-useradmin" data-type="add">职位发布登记</button>
            </div>

            <table id="LAY-user-manage" lay-filter="LAY-user-manage" style="text-align: center!important;"></table>

            <script type="text/html" id="table-useradmin-webuser">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="details"><i class="layui-icon layui-icon-list"></i>明细</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
            </script>

            <!--状态判断-->
<!--            <script type="text/html" id="statusTpl">
                {{#  if(d.status == 1){ }}
                <button class="layui-btn layui-btn-xs">正常</button>
                {{#  } else if(d.status == 2) { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">冻结</button>
                {{#  } }}

            </script>-->

            <!--  日期格式化处理 -->
            <script type="text/html" id="starTimeTpl">
                {{formatDate(d.regist_time)}}
            </script>
            <script type="text/html" id="endTimeTpl">
                {{formatDate(d.deadline)}}
            </script>

        </div>
    </div>
</div>

<!--<script src="js/jquery-2.0.3.min.js"></script>-->
<script src="../../../static/layui/layui.js"></script>
<script type="text/javascript">
    // 日期格式转换函数
    function formatDate(str) {
        if(str != null){
            var now = new Date(str);
            var year = now.getFullYear();  //取得4位数的年份
            var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
            var date = now.getDate();      //返回日期月份中的天数（1到31）
            return year + "-" + month + "-" + date;
        }
        return "";
    }
</script>
<script type="text/javascript">

    layui.use(['layer','table','form','element','util','laydate'], function(){
        var $ = layui.$
            ,form = layui.form
            ,layer = layui.layer
            ,table = layui.table
            ,element = layui.element
            ,util = layui.util
            ,laydate = layui.laydate;

        //头部事件
        util.event('lay-header-event', {
            //左侧菜单事件
            menuLeft: function(othis){
                layer.msg('展开左侧菜单的操作', {icon: 0});
            }
            ,menuRight: function(){
                layer.open({
                    type: 1
                    ,content: '<div style="padding: 80px;">处理右侧面板的操作</div>'
                    ,area: ['230px', '50%']
                    ,offset: 'rt' //右上角
                    ,anim: 5
                    ,shadeClose: true
                });
            }
        });
        //加载table
        table.render({
            elem: '#LAY-user-manage'
            ,url:'/recruit/jobPost/findAllPosts' //后端接口
            ,title: '职位发布信息表'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度
            ,cols: [[
                {type: 'checkbox', fixed: 'left',align:'center'}
                ,{field:'recruit_id',title:'招聘编号',hide:true}
                ,{field:'recruit_type',title:'招聘类型',hide:true}
                ,{field:'job_id',title:'职位编号',align:'center'}
                ,{field:'job_name', title:'职位名称',align:'center'}
                ,{field:'job_type', title:'职位分类',align:'center'}
                ,{field:'dept_id', title:'部门编号',hide:true}
                ,{field:'dept_short_name', title:'部门名称',align:'center'}
                ,{field:'resolution', title:'登记人',hide:true}
                ,{field:'recruit_number', title:'招聘人数',align:'center'}
                ,{field:'regist_time', title:'登记时间',align:'center',templet:'#starTimeTpl'}
                ,{field:'deadline', title:'截止时间',align:'center',templet:'#endTimeTpl'}
                ,{field:'description', title:'职位描述',hide:true}
                ,{field:'require', title:'招聘要求',hide:true}
                ,{fixed: 'right', title:'操作', toolbar: '#table-useradmin-webuser',width:280,align:'center'}
            ]]
            ,page: {
                curr: 1 //设定初始在第 5 页
                ,limit: 5 //设定初始每页显示 5 条
            },
            limits:[5,10,15,20]
        });

        //监听行工具事件
        table.on('tool(LAY-user-manage)', function(obj){
            var data = obj.data;
            //console.log(data.recruit_id)
            //delete
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        url:"/recruit/jobPost/delPost", //请求后端接口地址
                        type:"post", //请求方式get | post
                        data:{recruit_id:data.recruit_id}, //提交json字符串对象参数
                        dataType:"json", //后端返回参数类型
                        success:function (res){ //请求成功后回调函数res = JSONObject.toJSONString(resultMap)
                            if(res.code == 0){//成功
                                layer.msg(res.msg);//弹出提示
                                table.reload('LAY-user-manage'); //数据刷新
                                layer.close(index); //关闭弹层
                            }
                            if(res.code == -1){//系统出问题
                                layer.msg(res.msg,{icon: 5});//弹出提示
                            }
                        }
                    })
                });
            }
            //edit
            else if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '修改职位发布信息'
                    ,skin: 'layui-layer-molv'
                    ,content: 'editJobPost.html'
                    ,maxmin: true
                    ,area: ['1100px', '600px']
                    ,success: function (layero,index){
                        var body = layer.getChildFrame("body",index);
                        var contentWindow = layero.find('iframe')[0].contentWindow;//得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        body.find("input[name=recruit_id]").val(data.recruit_id);
                        body.find("input[name=dept_id]").val(data.dept_id);
                        body.find("input[name=dept_short_name]").val(data.dept_short_name);
                        /*body.find("input[name=recruit_type]").val(data.recruit_type);*/
                        //console.log(data.recruit_type)
                        body.find("input[name=recruit_number]").val(data.recruit_number);
                        body.find("input[name=job_name]").val(data.job_name);
                        body.find("input[name=job_id]").val(data.job_id);
                        body.find("input[name=job_type]").val(data.job_type);
                        body.find("input[name=resolution]").val(data.resolution);


                        var now = new Date(data.regist_time);
                        var year = now.getFullYear();  //取得4位数的年份
                        var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
                        var date = now.getDate();      //返回日期月份中的天数（1到31）
                        var regist_time = year + "-" + month + "-" + date;

                        var now2 = new Date(data.deadline);
                        var year2 = now2.getFullYear();  //取得4位数的年份
                        var month2 = now2.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
                        var date2 = now2.getDate();      //返回日期月份中的天数（1到31）
                        var deadline = year2 + "-" + month2 + "-" + date2;

                        body.find("input[name=regist_time]").val(regist_time);
                        body.find("input[name=deadline]").val(deadline);
                        body.find("textarea[name=description]").val(data.description);
                        body.find("textarea[name=require]").val(data.require);

                        // 获取角色值
                        var selectedRole = data.recruit_type;
                        // 设置下拉列表选中项
                        var roleSelect = body.find("select[name=recruit_type]");
                        roleSelect.val(selectedRole);
                        contentWindow.layui.form.render();
                    }
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var iframeWindow = window['layui-layer-iframe'+ index]
                            ,submitID = 'LAY-user-front-submit'
                            ,submit = layero.find('iframe').contents().find('#'+ submitID);
                        //console.log(submit)
                        //监听提交
                        iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                            var field = data.field; //获取提交的字段
                            //console.log(field)
                            $.ajax({
                                url:"/recruit/jobPost/editPost", //请求后端接口地址
                                type:"post", //请求方式get | post
                                data:JSON.stringify(field), //提交json字符串对象参数
                                contentType:"application/json;charset=utf-8", //发送信息至服务器时内容编码类型
                                dataType:"json", //后端返回参数类型
                                success:function (res){ //请求成功后回调函数res = JSONObject.toJSONString(resultMap)
                                    if(res.code == 0){//成功
                                        layer.msg(res.msg);//弹出提示
                                        table.reload('LAY-user-manage'); //数据刷新
                                        layer.close(index); //关闭弹层
                                    }
                                    if(res.code == -1){//系统出问题
                                        layer.msg(res.msg,{icon: 5});//弹出提示
                                    }
                                }
                            })
                        });
                        submit.trigger('click');
                    }
                });

            }
            //details
            else if(obj.event === 'details'){
                layer.open({
                    type: 2
                    ,title: '职位发布信息明细'
                    ,skin: 'layui-layer-molv'
                    ,content: 'detailsJobPost.html'
                    ,maxmin: true
                    ,area: ['1100px', '600px']
                    ,success: function (layero,index){
                        var body = layer.getChildFrame("body",index);
                        var window = layero.find('iframe')[0].contentWindow;//得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        body.find("input[name=dept_short_name]").val(data.dept_short_name).prop("readonly",true);
                        /*body.find("input[name=recruit_type]").val(data.recruit_type);*/
                        //console.log(data.recruit_type)
                        body.find("input[name=recruit_number]").val(data.recruit_number).prop("readonly",true);
                        body.find("input[name=job_name]").val(data.job_name).prop("readonly",true);
                        body.find("input[name=job_id]").val(data.job_id).prop("readonly",true);
                        body.find("input[name=job_type]").val(data.job_type).prop("readonly",true);
                        body.find("input[name=resolution]").val(data.resolution).prop("readonly",true);


                        var now = new Date(data.regist_time);
                        var year = now.getFullYear();  //取得4位数的年份
                        var month = now.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
                        var date = now.getDate();      //返回日期月份中的天数（1到31）
                        var regist_time = year + "-" + month + "-" + date;

                        var now2 = new Date(data.deadline);
                        var year2 = now2.getFullYear();  //取得4位数的年份
                        var month2 = now2.getMonth() + 1;  //取得日期中的月份，其中0表示1月，11表示12月
                        var date2 = now2.getDate();      //返回日期月份中的天数（1到31）
                        var deadline = year2 + "-" + month2 + "-" + date2;

                        body.find("input[name=regist_time]").val(regist_time).prop("readonly",true);
                        body.find("input[name=deadline]").val(deadline).prop("readonly",true);
                        body.find("textarea[name=description]").val(data.description).prop("readonly",true);
                        body.find("textarea[name=require]").val(data.require).prop("readonly",true);

                        body.find("option[value="+data.recruit_type+"]").attr("selected","selected").prop("readonly",true);
                        window.layui.form.render()
                    }
                    ,btn: ['返回']
                });

            }
        });


        //监听搜索
        form.on('submit(LAY-jobPost-front-search)', function(data){
            var field = data.field;
            console.log(field)
            // 调用后端接口，传递参数
            table.reload('LAY-user-manage', {
                url:"/recruit/jobPost/searchPost", //后端搜索接口URL地址
                where: field
            });
        });


        //事件
        var active = {
            batchdel: function(){
                var checkStatus = table.checkStatus('LAY-user-manage')
                    ,checkData = checkStatus.data; //得到选中的数据

                if(checkData.length == 0){
                    return layer.msg('请选择数据');
                }

                // 程序走到这里，表示已经有选中要删除的数据了。
                layer.confirm('确定删除吗？',function (){
                    //真实删除，应该和后端结合 ajax
                    var empnos = new Array();
                    $.each(checkData,function (index,item){
                        // 向数组对象中添加元素
                        empnos.push(item.empno);
                    })
                    $.ajax({
                        url:"/emp/batchdel",
                        type:"post",
                        data:{empnos:empnos},
                        traditional:true,
                        dataType:"json",
                        success:function (res){
                            if(res.code == 0){//成功
                                layer.msg(res.msg);//弹出提示
                                table.reload('LAY-user-manage'); //数据刷新
                            }
                            if(res.code == -1){//系统出问题
                                layer.msg(res.msg,{icon: 5});//弹出提示
                            }
                        }
                    })
                })
            }
            ,add: function(){
                window.location.href = "addJobPost.html"
            }
        };

        $('.layui-btn.layuiadmin-btn-useradmin').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        /*时间选择*/
        laydate.render({
            elem: '#test6'
            //设置开始日期、日期日期的 input 选择器
            //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
            ,range: ['#test-startDate-1', '#test-endDate-1']
        });
    });
</script>
</body>
</html>